﻿using System;
using UnityEngine;

public class ControlFactory<T> : IControlFactory<T> where T : Component {
	// === Public =====================================================================================================
	#region IControlFactory
	public void CreateControl() {
		if (!HasControl()) {
			_control = (IControl<T>) new GameObject(typeof(T).Name).AddComponent<T>();
			_control.Initialize();
		} else {
			throw new Exception("Control exists. Use 'DestroyControl()' before using this method");
		}
	}

	public void DestroyControl() {
		if (!HasControl()) {
			return;
		}
		_control.Destroy();
		_control = null;
	}

	public bool HasControl() {
		return _control != null;
	}

	public T GetControl() {
		return _control.GetControl();
	}
	#endregion

	// === Private ====================================================================================================
	private static IControl<T> _control;
}